package com.dream.web.controller;

import com.dream.dto.DataResponse;
import com.dream.entity.ConsoleConfig;
import com.dream.mapper.ConsoleConfigMapper;
import com.dream.web.aop.annotation.CommonResponse;
import io.swagger.annotations.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/api/common")
@Api(tags = "常规相关配置,免登录")
public class CommonController {

    @Resource
    private ConsoleConfigMapper consoleConfigMapper;

    @CommonResponse
    @GetMapping({"/getConfig/{group}","/getConfig/{group}/{key}"})
    @ApiOperation(value = "根据group或group& key获取配置json",response = DataResponse.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "key", value = "可选项[]", paramType = "path", required = true),
    @ApiImplicitParam(name = "group", value = "自由入参",paramType = "path", required = true) })
    public Object getConfigByKey(@PathVariable String group,@PathVariable(required = false) String key){
        ConsoleConfig consoleConfig = ConsoleConfig.builder().groupKey(group).build();
        if(!StringUtils.isEmpty(key)){consoleConfig.setOptionKey(key);}
        return consoleConfigMapper.selectOne(consoleConfig);
    }


    @CommonResponse
    @ApiOperation(value = "put控制台数据",response = DataResponse.class)
    @PostMapping("/putVal")
    public Object putConfigVal(@RequestBody ConsoleConfig consoleConfig){
        return consoleConfigMapper.insert(consoleConfig);
    }



}
